// packageStudent/pages/resume-list/index.js
import {getResumeList, delResume} from "../../../service/Student";
import {openDocument} from "../../../utils/Wx";
import {downloadFile, deleteFile} from "../../../service/Common"
import {getStorageSync} from "../../../utils/Storage";
import Config from "../../../main.config";
import Toast from "@vant/weapp/toast/toast";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        actionSheet: {
            show: false,
            list: [
                {name: "删除", index: 0}
            ]
        },
        //投递列表
        resume: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._registerInfo = getStorageSync(Config.storage.registerInfo);
        this.setData({"resume.loading": true});
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let registerInfo = this.data._registerInfo;
            let paging = e.detail.paging;
            let list = this.data.resume.list;
            let isReset = this.data.resume.reset;
            let res = await getResumeList({
                page: paging.page,
                limit: paging.pageSize,
                createUser: registerInfo.createUser
            });
            let newList = res.data.map(function (item) {
                return {
                    id: item.id,
                    name: item.name,
                    path: item.path
                }
            });
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "resume.list": list,
                "resume.total": res.count
            });
        } catch (e) {
            console.error(e)
            this.setData({
                "resume.error": true
            })
        } finally {
            this.setData({
                "resume.reset": false,
                "resume.loading": false
            })
        }
    },
    
    /*
    * 跳转上传简历页面
    * */
    bindNavigatorToAdd: async function() {
        const that = this;
        wx.navigateTo({
            url: "/packageStudent/pages/resume-add/index",
            events: {
                acceptDataFromOpenedPage: function(data) {
                    that.setData({"resume.reset": true})
                }
            },
        })
    },

    /*
    * 点击cell
    * */
    bindCellClick: async function (e) {
        try {
            let path = e.currentTarget.dataset.path;
            Toast.loading({
                message: "下载中...",
                duration: 0,
            });
            let tempFilePath = await downloadFile(path);
            Toast.clear();
            Toast.loading({
                message: "打开中...",
                duration: 0,
            });
            await openDocument(tempFilePath);
            Toast.clear();
        } catch (e) {
            Toast.fail("打开失败！");
            console.error(e)
        }
    },


    /*
    * 菜单
    * */
    bindCellLongPress: function (e) {
        this.data._deleteId = e.currentTarget.dataset.id;
        this.data._path = e.currentTarget.dataset.path;
        this.setData({"actionSheet.show": true})
    },
    onActionSheetClose: function () {
        this.setData({"actionSheet.show": false})
    },
    onActionSheetSelect: async function (e) {
        try {
            let deleteId = this.data._deleteId;
            let path = this.data._path;
            let index = e.detail.index;
            if (index !== 0) return false;
            Toast.loading({
                message: "删除中...",
                mask: true,
                duration: 0
            });
            await delResume(deleteId);
            await deleteFile(path);
            this.setData({"resume.reset": true});
            Toast.clear();
        } catch (e) {
            Toast.fail("删除失败");
            console.error(e);
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"resume.loading": true});
    }
})